//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OneFactorGaussianCopula.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Quote.h>
#include <gen/QL/Experimental/Credit/Distribution.h>
#include <gen/QL/Experimental/Credit/OneFactorCopula.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (Cephei::QL::IQuote^ correlation, Microsoft::FSharp::Core::FSharpOption<Double>^ maximum, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationSteps) : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
    CQuote^ _Ccorrelation;
    try
    {
#ifdef HANDLE
        _phOneFactorGaussianCopula = NULL;
#endif
        _Ccorrelation = safe_cast<CQuote^> (correlation);
        _Ccorrelation->Lock();
        Handle<QuantLib::Quote>& _correlation = static_cast<Handle<QuantLib::Quote>&> (_Ccorrelation->GetHandle ()); 
        QuantLib::Real _maximum = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (maximum) ? (QuantLib::Real)ValueHelper::Convert (maximum->Value) : 5); //4
        QuantLib::Size _integrationSteps = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (integrationSteps) ? (QuantLib::Size)ValueHelper::Convert (integrationSteps->Value) : 50); //4
        _ppOneFactorGaussianCopula = new boost::shared_ptr<QuantLib::OneFactorGaussianCopula> (new QuantLib::OneFactorGaussianCopula ( _correlation,  _maximum,  _integrationSteps ));
        SetOneFactorCopula (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccorrelation != nullptr) _Ccorrelation->Unlock();
    }
}
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (boost::shared_ptr<QuantLib::OneFactorGaussianCopula>& childNative, Object^ owner) : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
#ifdef HANDLE
	_phOneFactorGaussianCopula = NULL;
#endif
	_ppOneFactorGaussianCopula = &childNative;
    _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
}
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (QuantLib::OneFactorGaussianCopula& childNative, Object^ owner) : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
#ifdef HANDLE
	_phOneFactorGaussianCopula = NULL;
#endif
	_ppOneFactorGaussianCopula = new boost::shared_ptr<QuantLib::OneFactorGaussianCopula> (&childNative);
    _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
    _OneFactorGaussianCopulaOwner = owner;
    _OneFactorCopulaOwner = owner;
}

Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (COneFactorGaussianCopula^ copy) : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
#ifdef HANDLE
	_phOneFactorGaussianCopula = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOneFactorGaussianCopula = new boost::shared_ptr<QuantLib::OneFactorGaussianCopula> (copy->GetShared());
        _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
    }
}
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (System::Type^ t) : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
#ifdef HANDLE
	_phOneFactorGaussianCopula = NULL;
#endif
	if (!t->IsSubclassOf(COneFactorGaussianCopula::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (QuantLib::Handle<QuantLib::OneFactorGaussianCopula>& childNative, Object^ owner)  : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
	_phOneFactorGaussianCopula = &childNative;
	_ppOneFactorGaussianCopula = &static_cast<boost::shared_ptr<QuantLib::OneFactorGaussianCopula>>(childNative.currentLink());
    _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
    _OneFactorGaussianCopulaOwner = owner;
}
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (QuantLib::Handle<QuantLib::OneFactorGaussianCopula> childNative)  : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
	_phOneFactorGaussianCopula = &childNative;
	_ppOneFactorGaussianCopula = &static_cast<boost::shared_ptr<QuantLib::OneFactorGaussianCopula>>(childNative.currentLink());
    _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::COneFactorGaussianCopula (QuantLib::OneFactorGaussianCopula childNative)  : COneFactorCopula(COneFactorGaussianCopula::typeid)
{
#ifdef HANDLE
	_phOneFactorGaussianCopula = NULL;
#endif
	_ppOneFactorGaussianCopula = new boost::shared_ptr<QuantLib::OneFactorGaussianCopula> (new QuantLib::OneFactorGaussianCopula (childNative));
    _ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (boost::dynamic_pointer_cast<QuantLib::OneFactorCopula> (*_ppOneFactorGaussianCopula));
}
#endif

Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::~COneFactorGaussianCopula ()
{
    if (_ppOneFactorGaussianCopula != NULL)
    {
	    delete _ppOneFactorGaussianCopula;
        _ppOneFactorGaussianCopula = NULL;
    }
}
Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::!COneFactorGaussianCopula ()
{
    if (_ppOneFactorGaussianCopula != NULL)
    {
	    delete _ppOneFactorGaussianCopula;
    }
}
QuantLib::OneFactorGaussianCopula& Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::GetReference ()
{
    if (_ppOneFactorGaussianCopula == NULL) throw gcnew NativeNullException ();
	return **_ppOneFactorGaussianCopula;
}
boost::shared_ptr<QuantLib::OneFactorGaussianCopula>& Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::GetShared ()
{
    if (_ppOneFactorGaussianCopula == NULL) throw gcnew NativeNullException ();
	return *_ppOneFactorGaussianCopula;
}
QuantLib::OneFactorGaussianCopula* Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::GetPointer ()
{
    if (_ppOneFactorGaussianCopula == NULL) throw gcnew NativeNullException ();
	return &**_ppOneFactorGaussianCopula;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OneFactorGaussianCopula>& Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::GetHandle ()
{
	if (_phOneFactorGaussianCopula == NULL)
	{
		_phOneFactorGaussianCopula = new Handle<QuantLib::OneFactorGaussianCopula> (*_ppOneFactorGaussianCopula);
	}
	return *_phOneFactorGaussianCopula;
}
#endif
bool Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::HasNative () 
{
	return (_ppOneFactorGaussianCopula != NULL);
}

Double Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::CumulativeY (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorGaussianCopula)->cumulativeY ( _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::CumulativeZ (Double z)
{
    try
    {
        QuantLib::Real _z = (QuantLib::Real)ValueHelper::Convert (z);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorGaussianCopula)->cumulativeZ ( _z );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::Density (Double m)
{
    try
    {
        QuantLib::Real _m = (QuantLib::Real)ValueHelper::Convert (m);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorGaussianCopula)->density ( _m );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::InverseCumulativeY (Double p)
{
    try
    {
        QuantLib::Real _p = (QuantLib::Real)ValueHelper::Convert (p);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorGaussianCopula)->inverseCumulativeY ( _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorGaussianCopula::TestCumulativeY (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorGaussianCopula)->testCumulativeY ( _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IOneFactorGaussianCopula^ Cephei::QL::Experimental::Credit::COneFactorGaussianCopula_Factory::Create (Cephei::QL::IQuote^ correlation, Microsoft::FSharp::Core::FSharpOption<Double>^ maximum, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationSteps)
{
    return gcnew COneFactorGaussianCopula ( correlation,  maximum,  integrationSteps);
}
